package com.ladlee.handle;

import com.ladlee.domain.Result;
import com.ladlee.enums.ResultEnum;
import com.ladlee.utils.ResultUtils;
import com.ladlee.validata.exception.ValidateCodeException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Auther: dingchang
 * @Date: 2018/12/22 15:23
 * @Description:
 */
@ControllerAdvice
@Slf4j
public class GolbelException {

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result handle(Exception e){
        if (e instanceof ValidateCodeException) {
            ValidateCodeException validateCodeException = (ValidateCodeException) e;
            return ResultUtils.error(validateCodeException.getCode(), validateCodeException.getMessage());
        } else if (e instanceof AccessDeniedException) {
            return ResultUtils.error(ResultEnum.ACCESS_DENIED.getCode(), ResultEnum.ACCESS_DENIED.getMsg());
        } else {
            log.error("【系统异常】{}", e);
            return ResultUtils.error(-1, "未知错误");
        }
    }
}
